let checkValue = function (value) {
  if (parseInt(value) == 0) {
    return true;
  }
  return ((value == "" || value == false || value == null || value == undefined) ? false : true);
}

Number.prototype.toPercent = function(){
    return (Math.round(this * 10000)/100).toFixed(2) + '%';
}

Number.prototype.toFloat2 = function(){
    return (Math.round(this * 10000)/100).toFixed(2);
}

Number.prototype.toInt = function(){
    return Math.ceil((Math.round(this * 10000)/100).toFixed(2) / 100);
}

String.prototype.firstUpperCase = function(){
    return this.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
        return $1.toUpperCase() + $2.toLowerCase();
    });
}

// 检测是否是数字
let checkRate = function (input) {
　var re = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
  return (!re.test(input) ? false : true);
}

let url = window.location.protocol + "//"+ (window.location.hostname == "localhost" ? "www.xinxingtianxia.cn" : window.location.hostname) +"/api";

let get = function (path, params={}, callback) {
  params.token = $.cookie('admin_token');
  $.get(url + path, params, function (res) {
    callback(res);
  })
}

let post = function (path, params={}, callback) {
  params.token = $.cookie('admin_token');
  $.post(url + path, params, function (res) {
    callback(res);
  })
}

let common_url = "http://"+ (window.location.hostname == "localhost" ? "www.xinxingtianxia.cn" : window.location.hostname) +"/common/v1";
let common_post = function (path, params={}, callback) {
  params.token = $.cookie('admin_token');
  $.post(common_url + path, params, function (res) {
    callback(res);
  })
}

let post_image = function (path, data, callback) {
  data.append("token", $.cookie('admin_token'));
  $.ajax({
      type: "POST",
      url: url + path,
      data: data,
      cache: false,
      contentType: false,
      processData: false,
      async: false,
      success: function(res) {
        if (parseInt(res.code) == 200) {
          callback(res.url);
        }
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        $('.modal-body').text('上传失败，请检查网络后重试');
        $('#myModal').modal('show');
      }
  });
}

function timeConverter(date){
    var date = new Date(date*1000);//如果date为13位不需要乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y+M+D+h+m+s;
}

function TimeFormat(date){
    var date = new Date(date*1000);//如果date为13位不需要乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes());
    return Y+M+D+h+m;
}

function TimeFormatCopy(date){
    var date = new Date(date*1000);//如果date为13位不需要乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '/';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) +"/";
    return M+D+Y;
}

function TimeContact(date_arr) {
  var Y = date_arr[0];
  var M = (date_arr[1]+1 < 10 ? '0'+(date_arr[1]+1) : date_arr[1]+1) + '/';
  var D = (date_arr[2] < 10 ? '0' + (date_arr[2]) : date_arr[2]) + "/";
  return M+D+Y;
}


let area = [
    "北京",
    "上海",
    "天津",
    "重庆",
    "河北",
    "山西",
    "河南",
    "辽宁",
    "吉林",
    "黑龙江",
    "内蒙古",
    "江苏",
    "山东",
    "安徽",
    "浙江",
    "福建",
    "湖北",
    "湖南",
    "广东",
    "广西",
    "江西",
    "四川",
    "海南",
    "贵州",
    "云南",
    "西藏",
    "陕西",
    "甘肃",
    "青海",
    "宁夏",
    "新疆",
    "台湾",
    "香港",
    "澳门",
    "港澳",
    "钓鱼岛"
]

export default {
  checkValue,
  checkRate,
  get,
  post,
  post_image,
  timeConverter,
  area,
  TimeFormat,
  TimeContact,
  TimeFormatCopy,
  url,
  common_post
}
